package com.googlecode.etl.kravas.processor;

import com.googlecode.etl.kravas.beans.Bean;
import org.springframework.batch.item.ItemProcessor;

/**
 * @author A_Reshetnikov
 * @since Date: 28.08.2013
 */
public class BeanProcessor implements ItemProcessor<Bean, Bean> {

    public static String extractValue(String keyword, String str) {
        if (str == null) return null;
        String res = null;
        String[] array = str.split(";");
        for (int i = 0; i < array.length; i++) {
            if (array[i].toLowerCase().contains(keyword.toLowerCase())) {
                res = array[i];
            }
        }
        if (res == null) return null;
        res = res.replaceAll("(?i)" + keyword, ""); //ignore case for keyword
        return res.trim();
    }

    public Bean process(Bean bean) throws Exception {
        // Top Keyword Density (Excluding grammar words)
        // material (6238) - model (5694) - hp (4573) - diameter (3841) - size (2967) - psi (2673) - operator (2598) - rpm (2558) - volts (2485) - partname (2448)
        bean.setMaterial(extractValue("material", bean.getStrBuildDescription()));
        bean.setModel(extractValue("model", bean.getStrBuildDescription()));
        bean.setHp(extractValue("hp", bean.getStrBuildDescription()));
        bean.setDiameter(extractValue("diameter", bean.getStrBuildDescription()));
        bean.setSize(extractValue("size", bean.getStrBuildDescription()));
        bean.setPsi(extractValue("psi", bean.getStrBuildDescription()));
        bean.setOperator(extractValue("size", bean.getStrBuildDescription()));
        bean.setRpm(extractValue("rpm", bean.getStrBuildDescription()));
        bean.setVolts(extractValue("volts", bean.getStrBuildDescription()));
        bean.setPartname(extractValue("partname", bean.getStrBuildDescription()));

        // bean.setAddValue01("add_value01");
        return bean;
    }
}
